// components/searchBar/searchBar.js
let app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    doctorsTitle: {
      type: String,
      value: '合作类型筛选'
    },
    doctorsPlaceholder: {
      type: String,
      value: '请选择机合作类型'
    },
    docTitles: {
      type: String,
      value: '负责人业务员筛选'
    },
    docPlaceholders: {
      type: String,
      value: '请选择负责人业务员'
    },
    docterritoryTitles: {
      type: String,
      value: '地域筛选'
    },
    docterritoryPlaceholders: {
      type: String,
      value: '请选择地域'
    }
  },
  lifetimes: {
    ready() {
      const _this = this
      app.on('search', this, function (value) {
        _this.setData({
          value: value
        })
      })
      if (wx.getStorageSync('tabNav') == 3) {
        _this.getList()
        _this.getdisList()
      }
      // wx.request({
      //   url: app.globalData.serverUrlgdb + '/applet/treat/list',
      //   method: 'GET',
      //   header: {
      //     'token': wx.getStorageSync('token')
      //   },
      //   success:(res=>{
      //    let resData = res.data
      //    if(resData.code==200){
      //      console.log(resData.data.treatLists);
      //      resData.data.treatLists.map(item=>{
      //        item.label = item.treatmentName
      //        item.value = item.treatmentId
      //      })
      //      _this.setData({
      //        options:resData.data.treatLists
      //      })
      //    }
      //   })
      // })
    },

  },
  /**
   * 组件的初始数据
   */
  data: {
    showFilter: false,
    showFilters: false,
    doctors: "",
    territory: "",
    territoryActive: false,
    medicalActive: false,
    medicalActives: false,
    treatment: '',
    treatmentActive: false,
    tabNav: wx.getStorageSync('tabNav'),
    options: [{
        value: 0,
        label: '销售试用中'
      },
      {
        value: 1,
        label: '销售试用结束'
      },
      {
        value: 2,
        label: '销售运行'
      },
      {
        value: 3,
        label: '合作试用中'
      },
      {
        value: 4,
        label: '合作试用结束'
      },
      {
        value: 5,
        label: '合作运行'
      },
    ],
    bottom: '',
    treatmentBottom: '',
    territoryBottom: '',
    doctorsId: '',
    treatmentId: '',
    territoryId: '',
    salesPersonList: [],
    territoryPersonList: [],
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getdisList() {
      let that = this
      wx.request({
        url: app.globalData.serverUrlgdb + '/district/province/relate/list',
        method: 'GET',
        header: {
          'token': wx.getStorageSync('token')
        },
        success: res => {
          let resp = res.data
          if (resp.code == 200) {
            resp.data.provinceList.map(item => {
              item.value = item.id
              item.label = item.name
              item.checked = false
            })
            that.setData({
              territoryPersonList: resp.data.provinceList
            })
          }
        }
      })
    },
    getList() {
      let that = this
      wx.request({
        url: (wx.getStorageSync('tabNav') == 3 ? app.globalData.serverUrlgdb : app.globalData.serverUrl) + '/applet/get/person',
        method: 'get',
        header: {
          'token': wx.getStorageSync('token')
        },
        success: res => {
          let resp = res.data
          let salesPersonList = []
          if (resp.code == 200) {
            if (resp.data.salesPersonList.length != 0) {
              resp.data.salesPersonList.map(item => {
                let obj = {}
                obj.label = item
                obj.value = item
                salesPersonList.push(obj)
              })
              that.setData({
                salesPersonList
              })
            }
          }
        }
      })
    },
    bindtapFilter() {
      this.setData({
        showFilter: !this.data.showFilter,
        showFilters: true
      })
      let _this = this
      wx.createSelectorQuery().in(this).select('#medical').boundingClientRect(function (rect) {
        _this.setData({
          bottom: rect?.bottom
        })
      }).exec()
      wx.createSelectorQuery().in(this).select('#treatment').boundingClientRect(function (rect) {
        _this.setData({
          treatmentBottom: rect?.bottom
        })
      }).exec()
      wx.createSelectorQuery().in(this).select('#territory').boundingClientRect(function (rect) {
        _this.setData({
          territoryBottom: rect?.bottom
        })
      }).exec()
    },
    bindtapMedical() {
      this.setData({
        treatmentActive: false,
        medicalActive: !this.data.medicalActive
      })
    },
    bindChangeMedical(e) {
      this.setData({
        doctors: e.detail.label,
        doctorsId: e.detail.value,
        medicalActive: false
      })
    },
    bindtapTreatment() {
      this.setData({
        medicalActive: false,
        treatmentActive: !this.data.treatmentActive
      })
    },
    bindtapterritory() {
      this.setData({
        medicalActive: false,
        territoryActive: !this.data.territoryActive
      })
    },
    bindChangeterritory(e) {
      this.setData({
        territory: e.detail.label,
        territoryId: e.detail.value,
        territoryActive: false
      })
    },

    bindChangeTreatment(e) {
      this.setData({
        treatment: e.detail.label,
        treatmentId: e.detail.value,
        treatmentActive: false
      })
    },
    /**
     * 用户点击重置
     */
    bindtapReset() {
      this.setData({
        doctors: '',
        doctorsId: '',
        treatment: '',
        treatmentId: '',
        territory: "",
        territoryId: '',
      })
      this.triggerEvent('change', {
        doctors: '',
        doctorsId: '',
        treatment: '',
        treatmentId: '',
        territory: "",
        territoryId: '',
      })
    },
    /**
     * 用户点击确定
     */
    bindtapConfirm() {
      const {
        doctors,
        doctorsId,
        treatment,
        treatmentId,
        territory,
        territoryId
      } = this.data
      this.triggerEvent('change', {
        doctors: doctors,
        doctorsId: doctorsId,
        treatment: treatment,
        treatmentId: treatmentId,
        territory: territory,
        territoryId: territoryId,
      })
      this.setData({
        showFilter: false,
        showFilters: doctorsId == '' ? false : true,
      })
    }
  }
})